Vue.createApp({
	data() {
		return {
			list: [],
			level: [{
					name: '基础',
					row: 9, //行数
					col: 9, //列数
					count: 10
				},
				{
					name: '中级',
					row: 20, //行数
					col: 20, //列数
					count: 40
				},
				{
					name: '高级',
					row: 40, //行数
					col: 40, //列数
					count: 200
				}
			],
			current: 0,
			status: 'on',
			count:0,
			m:0,
			s:0,
			timer:null ,//计时器
			num:0
		}
	},
	created() {
		this.init()
	},
	watch:{
		num(){
			if(this.num ==1){
				this.startTime()
			}
		}
	},
	methods: {
		startTime(){
			this.timer=setInterval(()=>{
				this.s++
				if(this.s>=60){
					this.m++
					this.s=0
				}
			},1000)
		},
		init() {
			this.m=0,this.s=0
			let level = this.level[this.current] //当前等级信息
			this.count=level.count
			var list = [],
				indexList = [] //1、初始化表格
			for (let i = 0; i < level.row; i++) {
				list.push([]) //创建行
				for (let j = 0; j < level.col; j++) {
					let neb = [] //上 右上 右 下 左下 左 左上
					// 当前格子i,j
					neb.push({
						row: i - 1,
						col: j
					})
					neb.push({
						row: i - 1,
						col: j + 1
					})
					neb.push({
						row: i,
						col: j + 1
					})
					neb.push({
						row: i + 1,
						col: j + 1
					})
					neb.push({
						row: i + 1,
						col: j
					})
					neb.push({
						row: i + 1,
						col: j - 1
					})
					neb.push({
						row: i,
						col: j - 1
					})
					neb.push({
						row: i - 1,
						col: j - 1
					})
					list[i].push({
						content: 0,
						neb: neb, //邻居格子
						closed: 1,
						flag: 0
					})
					//所有格子的坐标信息
					indexList.push({
						row: i,
						col: j
					})
				}
			}
			console.log(list)
			console.log(indexList)
			//2、生成雷
			for (let i = 0; i < level.count; i++) {
				let x = Math.floor(Math.random() * indexList.length)
				let r = indexList[x].row,
					c = indexList[x].col
				list[r][c].content = -1
				indexList.splice(x, 1)
			}
			//3填数字
			for (let i = 0; i < list.length; i++) {
				for (let j = 0; j < list[i].length; j++) {
					if (list[i][j].content != -1) {
						let neb = list[i][j].neb
						let c = 0
						neb.forEach(item => {
							let row = item.row,
								col = item.col

							if (list[row] && list[row][col] != undefined && list[row][col].content == -
								1) {
								c++
							}
						})
						list[i][j].content = c
					}
				}
			}
			this.list = list
		},
		open(item) { //翻开格子
			if (this.status == 'off') return
			if (!item.closed || item.flag) return //已经打开的格子
			item.closed = 0
			this.num++
			if (item.content == -1) { //遇到雷
				this.status = 'off'
				clearInterval(this.timer)
				this.list.forEach(row => {
					row.forEach(col => {
						if (col.content == -1) {
							col.closed = 0
						}
					})
				})
				// alert('游戏结束')
				return
			}
			if (item.content == 0) {
				//翻到空白格
				item.neb.forEach(gz => {
					let r = gz.row,
						c = gz.col
					if (this.list[r] && this.list[r][c] != undefined) {
						this.open(this.list[r][c])

					}
				})
			}
		    this.isSuccess()
		},
		point(item) {
			this.num++
			if (item.flag) {
				item.flag = 0
				this.count++
			} else {
				item.flag = 1
				this.count--
			}
		},
		openSafe(item) {
			for (let i = 0; i < item.neb.length; i++) {
				if (this.isExist(item.neb[i])) {
					let gz = this.list[item.neb[i].row][item.neb[i].col]
					if (gz.closed && !gz.flag) {
						if (gz.content == -1) {
							console.log('不安全')
							return
						}
					}
				}
			}
			console.log('安全')
			for (let i = 0; i < item.neb.length; i++) {
				if (this.isExist(item.neb[i])) {
					let gz = this.list[item.neb[i].row][item.neb[i].col]
					if (gz.closed && !gz.flag) {
						this.open(gz)
					}
				}
			}
		},
		isExist(pos) {
			let r = pos.row,
				c = pos.col
			if (this.list[r] && this.list[r][c] != undefined) {
				return true
			}
			return false
		},
		isSuccess() {
			let level=this.level[this.current]
			let total=0
			this.list.forEach(row=>{
				row.forEach(col=>{
					if(col.closed==0)total++
				})
			})
			if(total+level.count==level.row*level.col){
				alert('恭喜你，挑战成功')
				this.status='off'
				clearInterval(this.timer)
			}
		}
	},
}).mount('#app')
